﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CuitHtmlControl.cs" company="">
//   
// </copyright>
// <summary>
//   The cuit html control.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace UITest.CUIT.CuitControls.HtmlControls
{
    using System;

    using Microsoft.VisualStudio.TestTools.UITesting;
    using Microsoft.VisualStudio.TestTools.UITesting.HtmlControls;

    using UITest.CUIT.CuitControls.Exceptions;
    using UITest.CUIT.CuitControls.Extension;

    /// <summary>
    /// The cuit html control.
    /// </summary>
    public class CuitHtmlControl : CuitControlBase<HtmlControl>
    {
    }

    /// <summary>
    /// The html control.
    /// </summary>
    /// <typeparam name="T">
    /// </typeparam>
    public class CuitHtmlControl<T> : CuitControlBase<T>, ICuitHtmlControl
        where T : HtmlControl
    {
        #region Constructors and Destructors

        /// <summary>
        ///     Initializes a new instance of the <see cref="CuitHtmlControl{T}" /> class.
        /// </summary>
        public CuitHtmlControl()
            : this(new UITestControl(), string.Empty)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CuitHtmlControl{T}"/> class.
        /// </summary>
        /// <param name="testControl">
        /// The test control.
        /// </param>
        public CuitHtmlControl(UITestControl testControl)
            : this(testControl, string.Empty)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CuitHtmlControl{T}"/> class.
        /// </summary>
        /// <param name="searchProperties">
        /// The search properties.
        /// </param>
        public CuitHtmlControl(string searchProperties)
            : this(new UITestControl(), searchProperties)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CuitHtmlControl{T}"/> class.
        /// </summary>
        /// <param name="testControl">
        /// The test control.
        /// </param>
        /// <param name="searchProperties">
        /// The search properties.
        /// </param>
        public CuitHtmlControl(UITestControl testControl, string searchProperties)
        {
            this.searchPropertiesInString = searchProperties;
            this.CreateControls(testControl, searchProperties);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CuitHtmlControl{T}"/> class.
        /// </summary>
        /// <param name="wrappedControl">
        /// The wrapped Control.
        /// </param>
        /// <param name="searchProperties">
        /// The search properties.
        /// </param>
        public CuitHtmlControl(ICuitControlBase wrappedControl, string searchProperties)
            : this(wrappedControl.GetMicrosoftTestControl(), searchProperties)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CuitHtmlControl{T}"/> class.
        /// </summary>
        /// <param name="wrappedControl">
        /// The wrapped Control.
        /// </param>
        /// <param name="searchProperties">
        /// The search properties.
        /// </param>
        /// <param name="filterProperties">
        /// The filter Properties.
        /// </param>
        public CuitHtmlControl(ICuitControlBase wrappedControl, string searchProperties, string filterProperties)
            : this(wrappedControl.GetMicrosoftTestControl(), searchProperties, filterProperties)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CuitHtmlControl{T}"/> class.
        /// </summary>
        /// <param name="testControl">
        /// The test control.
        /// </param>
        /// <param name="searchProperties">
        /// The search properties.
        /// </param>
        /// <param name="filterProperties">
        /// The filter Properties.
        /// </param>
        public CuitHtmlControl(UITestControl testControl, string searchProperties, string filterProperties)
        {
            this.searchPropertiesInString = searchProperties;
            this.filterPropertiesInString = filterProperties;
            this.CreateControls(testControl, searchProperties);
            this.SetFilterProperties(filterProperties);
        }

        #endregion

        #region Public Properties

        /// <summary>
        ///     Gets the inner text.
        /// </summary>
        public string InnerText
        {
            get
            {
                this._control.WaitForControlReady();
                return this._control.InnerText;
            }
        }

        /// <summary>
        ///     Gets the parent.
        /// </summary>
        /// <exception cref="CuitInvalidTraversal">
        /// </exception>
        public override ICuitControlBase Parent
        {
            get
            {
                this._control.WaitForControlReady();
                ICuitControlBase ret = null;
                try
                {
                    ret = this.WrapUtil((HtmlControl)this._control.GetParent());
                }
                catch (ArgumentOutOfRangeException)
                {
                    throw new CuitInvalidTraversal(string.Format("({0}).Parent", this._control.GetType().Name));
                }

                return ret;
            }
        }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The Creating the Controls
        /// </summary>
        /// <param name="testControl">
        /// The test control.
        /// </param>
        /// <param name="searchProperties">
        /// The search properties.
        /// </param>
        /// <returns>
        /// The <see cref="T"/>.
        /// </returns>
        /// <exception cref="Exception">
        /// </exception>
        public T CreateControls(UITestControl testControl, string searchProperties)
        {
            object htmlControl;

            if (typeof(T) == typeof(HtmlButton))
            {
                htmlControl = new HtmlButton(testControl);
            }
            else if (typeof(T) == typeof(HtmlCheckBox))
            {
                htmlControl = new HtmlCheckBox(testControl);
            }
            else if (typeof(T) == typeof(HtmlComboBox))
            {
                htmlControl = new HtmlComboBox(testControl);
            }
            else if (typeof(T) == typeof(HtmlDiv))
            {
                htmlControl = new HtmlDiv(testControl);
            }
            else if (typeof(T) == typeof(HtmlEdit))
            {
                htmlControl = new HtmlEdit(testControl);
            }
            else if (typeof(T) == typeof(HtmlEditableDiv))
            {
                htmlControl = new HtmlEditableDiv(testControl);
            }
            else if (typeof(T) == typeof(HtmlFileInput))
            {
                htmlControl = new HtmlFileInput(testControl);
            }
            else if (typeof(T) == typeof(HtmlFrame))
            {
                htmlControl = new HtmlFrame(testControl);
            }
            else if (typeof(T) == typeof(HtmlHyperlink))
            {
                htmlControl = new HtmlHyperlink(testControl);
            }
            else if (typeof(T) == typeof(HtmlImage))
            {
                htmlControl = new HtmlImage(testControl);
            }
            else if (typeof(T) == typeof(HtmlInputButton))
            {
                htmlControl = new HtmlInputButton(testControl);
            }
            else if (typeof(T) == typeof(HtmlLabel))
            {
                htmlControl = new HtmlLabel(testControl);
            }
            else if (typeof(T) == typeof(HtmlList))
            {
                htmlControl = new HtmlList(testControl);
            }
            else if (typeof(T) == typeof(HtmlRadioButton))
            {
                htmlControl = new HtmlRadioButton(testControl);
            }
            else if (typeof(T) == typeof(HtmlSpan))
            {
                htmlControl = new HtmlSpan(testControl);
            }
            else if (typeof(T) == typeof(HtmlEditableSpan))
            {
                htmlControl = new HtmlEditableSpan(testControl);
            }
            else if (typeof(T) == typeof(HtmlTable))
            {
                htmlControl = new HtmlTable(testControl);
            }
            else if (typeof(T) == typeof(HtmlHeaderCell))
            {
                htmlControl = new HtmlHeaderCell(testControl);
            }
            else if (typeof(T) == typeof(HtmlCell))
            {
                htmlControl = new HtmlCell(testControl);
            }
            else if (typeof(T) == typeof(HtmlRow))
            {
                htmlControl = new HtmlRow(testControl);
            }
            else if (typeof(T) == typeof(HtmlTextArea))
            {
                htmlControl = new HtmlTextArea(testControl);
            }
            else if (typeof(T) == typeof(HtmlIFrame))
            {
                htmlControl = new HtmlIFrame(testControl);
            }
            else if (typeof(T) == typeof(HtmlCustom))
            {
                htmlControl = new HtmlCustom(testControl);
            }
            else
            {
                throw new Exception(string.Format("CreateControls: '{0}' not supported", typeof(T).Name));
            }

            // Casting the Object to microsoft Control
            this._control = (T)htmlControl;
            this._control.CuitSetSearchProperties<T>(searchProperties);

            return this._control;
        }

        /// <summary>
        /// The set filter properties.
        /// </summary>
        /// <param name="filterProperties">
        /// The filter properties.
        /// </param>
        /// <returns>
        /// The <see cref="T"/>.
        /// </returns>
        public T SetFilterProperties(string filterProperties)
        {
            this._control.CuitSetFilterProperties<T>(filterProperties);
            return this._control;
        }

        #endregion

        #region Methods

        private ICuitControlBase WrapUtil(HtmlControl control)
        {
            ICuitControlBase _con = null;

            if (control.GetType() == typeof(HtmlButton))
            {
                _con = new CuitHtmlButton();
            }
            else if (control.GetType() == typeof(HtmlCheckBox))
            {
                _con = new CuitHtmlCheckBox();
            }
            else if (control.GetType() == typeof(HtmlComboBox))
            {
                _con = new CuitHtmlComboBox();
            }
            else if (control.GetType() == typeof(HtmlDiv))
            {
                _con = new CuitHtmlDiv();
            }
            else if (control.GetType() == typeof(HtmlEdit))
            {
                _con = new CuitHtmlEdit();
            }
            else if (control.GetType() == typeof(HtmlEditableDiv))
            {
                _con = new CuitHtmlEditableDiv();
            }
            else if (control.GetType() == typeof(HtmlFileInput))
            {
                _con = new CuitHtmlFileInput();
            }
            else if (control.GetType() == typeof(HtmlFrame))
            {
                _con = new CuitHtmlFrame();
            }
            else if (control.GetType() == typeof(HtmlHyperlink))
            {
                _con = new CuitHtmlHyperLink();
            }
            else if (control.GetType() == typeof(HtmlImage))
            {
                _con = new CuitHtmlImage();
            }
            else if (control.GetType() == typeof(HtmlInputButton))
            {
                _con = new CuitHtmlInputButton();
            }
            else if (control.GetType() == typeof(HtmlLabel))
            {
                _con = new CuitHtmlLabel();
            }
            else if (control.GetType() == typeof(HtmlList))
            {
                _con = new CuitHtmlList();
            }
            else if (control.GetType() == typeof(HtmlRadioButton))
            {
                _con = new CuitHtmlRadioButton();
            }
            else if (control.GetType() == typeof(HtmlSpan))
            {
                _con = new CuitHtmlSpan();
            }
            else if (control.GetType() == typeof(HtmlEditableSpan))
            {
                _con = new CuitHtmlEditableSpan();
            }
            else if (control.GetType() == typeof(HtmlTable))
            {
                _con = new CuitHtmlTable();
            }
            else if (control.GetType() == typeof(HtmlRow))
            {
                _con = new CuitHtmlRow();
            }
            else if (control.GetType() == typeof(HtmlCell))
            {
                _con = new CuitHtmlCell();
            }
            else if (control.GetType() == typeof(HtmlHeaderCell))
            {
                _con = new CuitHtmlHeaderCell();
            }
            else if (control.GetType() == typeof(HtmlTextArea))
            {
                _con = new CuitHtmlTextArea();
            }
            else if (control.GetType() == typeof(HtmlIFrame))
            {
                _con = new CuitHtmlIFrame();
            }
            else if (control.GetType() == typeof(HtmlCustom))
            {
                _con = new CuitHtmlCustom();
            }
            else
            {
                throw new Exception(string.Format("WrapUtil: '{0}' not supported", control.GetType().Name));
            }

            _con.WrapReady(control);

            return _con;
        }

        #endregion
    }
}